package io.jt808.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;



public class Tools {

    public static String ToHexString(byte[] bts) {
        StringBuilder strBuild = new StringBuilder();

        for (byte bt : bts) {
            strBuild.append(ToHexString(bt));
        }
        return strBuild.toString();
    }

    public static String ToHexFormatString(byte[] bts) {
        StringBuilder strBuild = new StringBuilder();

        for (byte bt : bts) {
            strBuild.append(ToHexString(bt)).append(" ");
        }
        return strBuild.toString();
    }

    public static byte[] HexString2Bytes(String hexstr) {
        byte[] b = new byte[hexstr.length() / 2];
        int j = 0;

        for (int i = 0; i < b.length; i++) {
            char c0 = hexstr.charAt(j++);
            char c1 = hexstr.charAt(j++);

            b[i] = ((byte) (parse(c0) << 4 | parse(c1)));
        }

        return b;
    }

    private static int parse(char c) {
        if (c >= 'a') {
            return c - 'a' + 10 & 0xF;
        }

        if (c >= 'A') {
            return c - 'A' + 10 & 0xF;
        }

        return c - '0' & 0xF;
    }

    public static String ToHexString(byte data) {
        return Integer.toHexString(data & 0xff | 0x100).substring(1);
    }
    public static String getBcd(int... numbers) {
        if (numbers.length == 0) return "";
        StringBuilder sb = new StringBuilder();
        for (int n : numbers) {
            sb.append(String.format("%02d", n));
        }
        return sb.toString();
    }
    public static String getBcdDateTime(Date startTime) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(startTime.toInstant(), ZoneId.systemDefault());
        return Tools.getBcd(localDateTime.getYear() - 2000,
                localDateTime.getMonthValue(),
                localDateTime.getDayOfMonth(),
                localDateTime.getHour(),
                localDateTime.getMinute(),
                localDateTime.getSecond());
    }

    public static String getBcdDate(Date date) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
        return Tools.getBcd(20, localDateTime.getYear() - 2000,
                localDateTime.getMonthValue(),
                localDateTime.getDayOfMonth());
    }

    public static Date getDateTime(String year1, String month1, String day1, String hour1, String minute1, String second1) {
        Integer year = Integer.valueOf(year1) + 2000;
        Integer month = Integer.valueOf(month1);
        Integer day = Integer.valueOf(day1);
        Integer hour = Integer.valueOf(hour1);
        Integer minute = Integer.valueOf(minute1);
        Integer second = Integer.valueOf(second1);
        LocalDateTime localDateTime = LocalDateTime.of(year, month, day, hour, minute, second);
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    public static Date getDate(String year1, String year2, String month1, String day1) {
        Integer year = Integer.valueOf(year1 + year2);
        Integer month = Integer.valueOf(month1);
        Integer day = Integer.valueOf(day1);
        LocalDate localDate = LocalDate.of(year, month, day);
        LocalDateTime localDateTime = localDate.atStartOfDay();
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }
}
